<?php
declare (strict_types=1);

namespace app\admin\service\advertiser;

use app\admin\model\advertiser\AdvertiserUser;
use app\admin\model\advertiser\AdvertiserUserRecharge;
use app\admin\service\base\JwtService;
use app\BaseService;
use think\response\Json;

/**
 * 广告主用户管理
 */
class AdvertiserUserService extends BaseService
{

    /**
     * 广告主用户列表
     * @param array $params
     * @return Json
     */
    public function list(array $params): Json
    {
        $where[] = ['closed', '=', 0];
        //搜索名称
        if ($params['name']) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        //搜索手机号
        if ($params['phone']) {
            $where[] = ['phone', '=', $params['phone']];
        }
        //搜索状态
        if (isset($params['enable']) and in_array($params['enable'], [0, 1])) {
            $where[] = ['enable', '=', $params['enable']];
        }
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        $data = AdvertiserUser::getList($where, $params['page'], $params['limit'], 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 广告主全部用户列表
     * @param array $params
     * @return Json
     */
    public function listAll(array $params): Json
    {
        $where[] = ['closed', '=', 0];
        //搜索名称
        if ($params['name']) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        //获取列表
        $data = AdvertiserUser::getListAll($where, 'id,name', 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 广告主用户禁用或启用
     * @param int $id
     * @param int $enable
     * @return Json
     */
    public function enable(int $id, int $enable): Json
    {
        //判断参数
        if (!in_array($enable, [0, 1])) {
            return jsonReturn(0, [], '参数错误');
        }
        //查询广告主用户
        $where = ['id' => $id, 'closed' => 0];
        $Model = AdvertiserUser::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '广告主用户不存在');
        }
        //禁用执行退出
        if ($enable == 0) {
            JwtService::userAdminLogout($id, 'advertiser');
        }
        //编辑
        $params['enable'] = $enable;
        $params['update_time'] = time();
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, []);
        }
        return jsonReturn(0, [], '操作失败');
    }

    /**
     * 广告主充值
     * @param array $params
     * @return Json
     */
    public function recharge(array $params): Json
    {
        //验证参数
        if (!$params['ad_user_id'] or !$params['actual_pay'] or !$params['arrival_pay']) {
            return jsonReturn(0, [], '参数错误');
        }
        if ($params['actual_pay'] <= 0 or $params['arrival_pay'] <= 0) {
            return jsonReturn(0, [], '金额不能小于0');
        }
        //判断当前广告主是否存在
        $where_user[] = ['id', '=', $params['ad_user_id']];
        $where_user[] = ['closed', '=', 0];
        $User = AdvertiserUser::findOne($where_user);
        if (!$User) {
            return jsonReturn(0, [], '广告主不存在');
        }
        //开启事务
        AdvertiserUserRecharge::startTrans();
        $data = [
            'ad_user_id' => $params['ad_user_id'],
            'order_no' => date('YmdHis') . $params['ad_user_id'] . rand(100, 999),
            'mode' => 1,
            'actual_pay' => $params['actual_pay'],
            'arrival_pay' => $params['arrival_pay'],
            'admin_id' => WS_ADMIN_ID,
            'create_time' => time(),
        ];
        //写入充值记录
        $recharge_id = AdvertiserUserRecharge::insertGetId($data);
        //写入广告主余额
        $res = AdvertiserUser::where($where_user)->inc('balance', $params['arrival_pay'])->update();;
        if ($recharge_id and $res) {
            AdvertiserUserRecharge::commit();
            return jsonReturn(1, [], '充值成功');
        }
        AdvertiserUserRecharge::rollback();
        return jsonReturn(0, [], '充值失败');
    }

    /**
     * 广告主充值记录
     * @param array $params
     * @return Json
     */
    public function rechargeList(array $params): Json
    {
        $where = [];
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        $data = AdvertiserUserRecharge::getList($where, $params['page'], $params['limit'], 'id desc');
        return jsonReturn(1, $data);
    }


}
